﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace NewSeasonFinder
{
    public partial class ShowEditor : Form
    {
        private XmlDocument xmlDoc;
        private XmlNode xn_show;
        private bool isEdit;

        /// <summary>
        /// 剧集添加/编辑
        /// </summary>
        /// <param name="_xmlDoc">要加入剧集的XML文档</param>
        public ShowEditor(XmlDocument _xmlDoc)
        {
            InitializeComponent();
            xmlDoc = _xmlDoc;
            tbName.Text = "";
            tbChildName.Text = "";
            isEdit = false;
        }

        /// <summary>
        /// 剧集添加/编辑
        /// </summary>
        /// <param name="_xmlDoc">要编辑剧集的XML文档</param>
        /// <param name="_xnShow">要编辑的剧集</param>
        public ShowEditor(XmlDocument _xmlDoc, XmlNode _xnShow)
        {
            InitializeComponent();
            xmlDoc = _xmlDoc;
            xn_show = _xnShow;
            tbName.Text = ShowXml.GetShowName(xn_show);
            tbChildName.Text = ShowXml.GetShowChildName(xn_show);
            isEdit = true;
        }

        /// <summary>
        /// 确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonOK_Click(object sender, EventArgs e)
        {
            int result;
            if (!isEdit)
            {
                result = ShowXml.AddShow(xmlDoc, tbName.Text, tbChildName.Text);
            }
            else
            {
                result = ShowXml.EditShow(xmlDoc, xn_show, tbName.Text, tbChildName.Text);
            }

            if (result == 0)
            {
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Close();
            }
            else
            {
                string error = "未知错误";
                switch(result)
                {
                    case -1://名称为空; 
                        error = "剧集名不能为空";
                        break;
                    case -2://剧集重复; 
                        error = "剧集重复";
                        break;
                    case -3://剧集不存在
                        error = "要编辑的剧集已不存在";
                        break;
                    default:
                        error = "未知错误";
                        break;
                }
                MessageBox.Show(error, "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        /// <summary>
        /// 取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }


    }
}
